﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Xml.XPath;
using System.Xml.Xsl;
using InfoJetSoft.Service.Entity;
using System.Globalization;
using System.Text.RegularExpressions;

//CID 0755 //2011.01.06 //Begin
namespace InfoJetSoft.Service.Util
{
    class XPathEvaluator
    {
        //CID 0927 //2011.06.21 //Begin
        public static XPathNodeIterator evaluateNodeList(Form form, XmlNode contextNode, string expression)
        {
            XPathNavigator nav = contextNode.CreateNavigator();
            XPathExpression xpathExpr = nav.Compile(expression);
            XPathContext xpathContext = new XPathContext(form);
            xpathExpr.SetContext(xpathContext);
            object result = nav.Evaluate(xpathExpr);
            if (result is XPathNodeIterator)
            {
                XPathNodeIterator nodeIterator = result as XPathNodeIterator;
                return nodeIterator;
            }
            else
            {
                throw new InfoJetException(expression + " is not NodeList.", "XPathEvaluator.evaluateNodeList");
            }
        }
        //CID 0927 //2011.06.21 //End

        public static string evaluateStringValue(Form form, XmlNode contextNode, string expression)
        {
            XPathNavigator nav = contextNode.CreateNavigator();
            XPathExpression xpathExpr = nav.Compile(expression);
            XPathContext xpathContext = new XPathContext(form);
            xpathExpr.SetContext(xpathContext);
            object result = nav.Evaluate(xpathExpr);
            if (result is XPathNodeIterator)
            {
                XPathNodeIterator nodeIterator = result as XPathNodeIterator;
                if (nodeIterator.MoveNext())
                {
                    string content = nodeIterator.Current.Value;
                    return content;
                }
                else
                {
                    return "";
                }
            }
            else
            {
                //CID 0862 //2011.04.11 //Begin
                if (result is bool)
                {
                    bool boolResult = (bool)result;
                    if (boolResult)
                    {
                        return "true";
                    }
                    else
                    {
                        return "false";
                    }
                }
                else if (result is double)
                {
                    double doubleValue = (double)result;
                    //CID 1007 //2011.08.11 //Begin
                    return FloatingPointString.ToString(doubleValue);
                    //CID 1007 //2011.08.11 //End
                }
                else
                {
                    return result.ToString();
                }
                //CID 0862 //2011.04.11 //End
            }
        }

        public static bool evaluateBooleanValue(Form form, XmlNode contextNode, string expression)
        {
            XPathNavigator nav = contextNode.CreateNavigator();
            XPathExpression xpathExpr = nav.Compile("boolean(" + expression + ")");
            XPathContext xpathContext = new XPathContext(form);
            xpathExpr.SetContext(xpathContext);
            object result = nav.Evaluate(xpathExpr);
            if (result is bool)
            {
                return (bool)result;
            }
            else
            {
                throw new InfoJetException("Error Exception: " + expression, "XPE.evaluateBooleanValue");
            }
        }

    }
}
//CID 0755 //2011.01.06 //End